//
//  LocationManager.m
//  XiaoMiao
//
//  Created by jinglian on 2018/3/28.
//  Copyright © 2018年 smallyou. All rights reserved.
//
//  Description: <#文件描述#>
//  History:
//    1. 2018/3/28 [jinglian]: 创建文件;
//

#import "LocationManager.h"
#import "CoreLocation/CoreLocation.h"


static LocationManager *instance = nil;

@interface LocationManager() <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager* locationManager;
/** <#属性#> */
@property (nonatomic, copy) LocationBlock locationBlock;

@end

@implementation LocationManager

+ (LocationManager *)manager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[LocationManager alloc] init];
    });
    
    return instance;
}

- (void)startLocationLocationBlock:(LocationBlock)locationBlock {
    
    self.locationBlock = locationBlock;
    
    if ([CLLocationManager locationServicesEnabled]) {//判断定位操作是否被允许
        
        self.locationManager = [[CLLocationManager alloc] init];
        
        self.locationManager.delegate = self;//遵循代理
        
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        
        self.locationManager.distanceFilter = 10.0f;
        
        [_locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据（iOS8以上版本定位需要）
        
        [self.locationManager startUpdatingLocation];//开始定位
        
    }else{//不能定位用户的位置的情况再次进行判断，并给与用户提示
        
        //1.提醒用户检查当前的网络状况
        
        //2.提醒用户打开定位开关
        
        UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"无法定位" message:@"请在iPhone的“设置-隐私-定位”选项中，允许本应用程序访问你的定位。" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        
        [alertview show];

    }
}



#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    
    //当前所在城市的坐标值
    CLLocation *currLocation = [locations lastObject];
    
    NSLog(@"经度=%f 纬度=%f 高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);
    
    //根据经纬度反向地理编译出地址信息
    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
    
    [geoCoder reverseGeocodeLocation:currLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        
        for (CLPlacemark * placemark in placemarks) {
            
            NSDictionary *address = [placemark addressDictionary];
            
            //  Country(国家)  State(省)  City（市）
            NSLog(@"#####%@",address);
            
            NSLog(@"%@", [address objectForKey:@"Country"]);
            
            NSLog(@"%@", [address objectForKey:@"State"]);
            
            NSLog(@"%@", [address objectForKey:@"City"]);
            
            if (self.locationBlock) {
                self.locationBlock([address objectForKey:@"State"], [address objectForKey:@"City"]);
            }
        }
        
    }];
    
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    
    if ([error code] == kCLErrorDenied){
        [KLProgressHUD showPromptHUDWithMessage:@"暂未开启定位服务"];
    }
    if ([error code] == kCLErrorLocationUnknown) {
        [KLProgressHUD showPromptHUDWithMessage:@"无法获取位置信息"];
    }
}

@end
